package com.tianji.promotion.controller;


import com.tianji.api.dto.promotion.CouponDiscountDTO;
import com.tianji.api.dto.promotion.OrderCourseDTO;
import com.tianji.promotion.domain.po.Coupon;
import com.tianji.promotion.enums.DiscountType;
import com.tianji.promotion.service.IDiscountService;
import com.tianji.promotion.service.IUserCouponService;
import com.tianji.promotion.strategy.discount.DiscountStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户领取优惠券的记录，是真正使用的优惠券信息 前端控制器
 * </p>
 *
 * @author 东哥
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user-coupons")
@Api(tags = "优惠券相关接口")
public class UserCouponController {

    private final IUserCouponService userCouponService;
    private final IDiscountService discountService;

    /**
     * Day10 - 随堂2
     * @return
     */
    @ApiOperation("领取优惠券接口")
    @PostMapping("/{couponId}/receive")
    public void receiveCoupon(@PathVariable("couponId") Long couponId){
        userCouponService.receiveCoupon(couponId);
    }

    /**
     * Day10 - 随堂3
     * @return
     */
    @ApiOperation("兑换码兑换优惠券接口")
    @PostMapping("/{code}/exchange")
    public void exchangeCoupon(@PathVariable("code") String code){
        userCouponService.exchangeCoupon(code);
    }

    /**
     * Day12 - 随堂1
     *
     * @return
     */
    @ApiOperation("查询我的优惠券可用方案")
    @PostMapping("/available")
    public List<CouponDiscountDTO> findDiscountSolution(@RequestBody List<OrderCourseDTO> orderCourses) {
        return discountService.findDiscountSolution(orderCourses);
    }

    public void before() {
        // 1、从数据库查询可用优惠券
        Coupon c = new Coupon();

        // 2、计算订单总金额
        int totalAmount = 1000;

        // 3、判断优惠券是否能用于当前订单
        boolean canUse = false;
        if (c.getDiscountType() == DiscountType.NO_THRESHOLD) {
            canUse = totalAmount >= c.getDiscountValue();
        } else if (c.getDiscountType() == DiscountType.PRICE_DISCOUNT) {
            canUse = totalAmount >= c.getThresholdAmount();
        } else if (c.getDiscountType() == DiscountType.PER_PRICE_DISCOUNT) {
            canUse = totalAmount >= c.getThresholdAmount();
        } else if (c.getDiscountType() == DiscountType.RATE_DISCOUNT) {
            canUse = totalAmount >= c.getThresholdAmount();
        }
        // 4、如果能用，减多少？
        int discountAmount = 0;
        if (c.getDiscountType() == DiscountType.NO_THRESHOLD) {
            discountAmount = c.getDiscountValue();
        } else if (c.getDiscountType() == DiscountType.PRICE_DISCOUNT) {
            discountAmount = c.getDiscountValue();
        } else if (c.getDiscountType() == DiscountType.PER_PRICE_DISCOUNT) {
            int discount = totalAmount / c.getThresholdAmount() * c.getDiscountValue();
            discountAmount = Math.min(discount, c.getMaxDiscountAmount());
        } else if (c.getDiscountType() == DiscountType.RATE_DISCOUNT) {
            discountAmount = Math.min(c.getMaxDiscountAmount(), totalAmount * (100 - c.getDiscountValue()) / 100);
        }
        // 5、页面上如果要展示当前优惠券文字信息，该如何获取
        String msg = "";
        if (c.getDiscountType() == DiscountType.NO_THRESHOLD) {
            msg = "1";
        } else if (c.getDiscountType() == DiscountType.PRICE_DISCOUNT) {
            msg = "2";
        } else if (c.getDiscountType() == DiscountType.PER_PRICE_DISCOUNT) {
            msg = "3";
        } else if (c.getDiscountType() == DiscountType.RATE_DISCOUNT) {
            msg = "4";
        }
    }

    // 设计模式优势：编码变得非常优雅，符合程序设计的 开闭原则（对扩展开放，对修改关闭！！！）
    public void now() {
        // 1、从数据库查询可用优惠券
        Coupon c = new Coupon();

        // 2、计算订单总金额
        int totalAmount = 1000;

        // 3、判断优惠券是否能用于当前订单
        boolean canUse = DiscountStrategy.getDiscount(c.getDiscountType()).canUse(totalAmount,c);

        // 4、如果能用，减多少？
        int discountAmount = DiscountStrategy.getDiscount(c.getDiscountType()).calculateDiscount(totalAmount,c);

        // 5、页面上如果要展示当前优惠券文字信息，该如何获取
        String msg = DiscountStrategy.getDiscount(c.getDiscountType()).getRule(c);
    }
}
